<?php

class User_Action_Admin_Manage_Delete extends Action_Admin
{
	function execute($request, $initial = false, $script = null)
	{
		if ($initial = true)
		{
			Agilet::setVar('breadcrum', array(
				array(
					'url' => 'admin/user',
					'label' => 'User'
				),
				array(
					'url' => 'admin/user/manage',
					'label' => 'Manage'
				),
				array(
					'url' => 'admin/user/manage/delete',
					'label' => 'Delete'
				),
			));
		}

		$form = Form::factory('core/admin/delete');

		$id = (int)$request -> getParam('id');

		$table = Model::getTable('user');

		$user = $table -> find($id) -> current();

		if ($request -> isGet() && $user)
		{
			$array = $user -> toArray();
			$form -> setData($array);
		}

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$user -> delete();
			
			Response::redirect('admin/user/manage');
			$form->setNotice('Saved.');
		}
		$request -> response = View::render($script = 'core/html/admin/edit', $data = array(
			'form' => $form,
			'title' => 'Delete User'
		));
	}

}
